package org.example.im.server.pipeline;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.RequiredArgsConstructor;
import org.example.im.common.codec.PacketCodecHandler;
import org.example.im.common.codec.Spliter;
import org.example.im.common.idle.IdleHandler;
import org.example.im.server.handler.impl.AuthHandler;
import org.example.im.server.handler.BizHandlerFactory;
import org.example.im.server.handler.impl.HeartBeatHandler;
import org.example.im.server.handler.impl.LoginHandler;
import org.springframework.stereotype.Component;

/**
 * @author dingrui
 * @since 2022/2/10
 * @description
 */
@Component
@RequiredArgsConstructor
public class ServerInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
        // 服务端1个channel共用handler情况
        nioSocketChannel.pipeline()
                .addLast(new IdleHandler()) // 空闲
                .addLast(new Spliter()) // 拆包
                .addLast(new PacketCodecHandler()) // 编解码
                .addLast(new LoginHandler()) // 登录
                .addLast(new HeartBeatHandler()) // 心跳
                .addLast(new AuthHandler()) // 鉴权
                .addLast(new BizHandlerFactory()); // 业务逻辑
    }
}
